package com.fsnbigdata.service.impl;

import com.fsnbigdata.domain.Taxonomy;
import com.fsnbigdata.repository.TaxonomyRepository;
import com.fsnbigdata.service.api.HintService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.List;

/**
 * Created by zhongcheng on 12/10/2017.
 */
@Service
@Transactional(readOnly = true)
public class HintServiceImpl implements HintService{

    private TaxonomyRepository taxonomyRepository;

    @Autowired
    public HintServiceImpl(TaxonomyRepository taxonomyRepository) {
        this.taxonomyRepository = taxonomyRepository;
    }

    @Override
    public List<Taxonomy> findByKingdomLike(String kingdom) {
        return taxonomyRepository.findFirst1000ByKingdomLike(kingdom);
    }

    @Override
    public List<Taxonomy> findByKingdomAndSubKingdomLike(String kingdom, String subKingdom) {
        return taxonomyRepository.findFirst1000ByKingdomAndSubKingdomLike(kingdom, subKingdom);
    }

    @Override
    public List<Taxonomy> findByKingdomAndPhylumnLike(String kingdom, String phylumn) {
        return taxonomyRepository.findFirst1000ByKingdomAndPhylumnLike(kingdom, phylumn);
    }

    @Override
    public List<Taxonomy> findByPhylumnAndSubPhylumnLike(String phylumn, String subPhylumn) {
        return taxonomyRepository.findFirst1000ByPhylumnAndSubPhylumnLike(phylumn, subPhylumn);
    }

    @Override
    public List<Taxonomy> findByPhylumnAndTClassLike(String phylumn, String tClass) {
        return taxonomyRepository.findFirst1000ByPhylumnAndTClassLike(phylumn, tClass);
    }

    @Override
    public List<Taxonomy> findByTClassAndSubClassLike(String tClass, String subClass) {
        return taxonomyRepository.findFirst1000ByTClassAndSubClassLike(tClass, subClass);
    }

    @Override
    public List<Taxonomy> findByTClassAndTOrderLike(String tClass, String tOrder) {
        return taxonomyRepository.findFirst1000ByTClassAndTOrderLike(tClass, tOrder);
    }

    @Override
    public List<Taxonomy> findByTOrderAndSubOrderLike(String tOrder, String subOrder) {
        return taxonomyRepository.findFirst1000ByTOrderAndSubOrderLike(tOrder, subOrder);
    }

    @Override
    public List<Taxonomy> findByTOrderAndFamilyLike(String tOrder, String family) {
        return taxonomyRepository.findFirst1000ByTOrderAndFamilyLike(tOrder, family);
    }

    @Override
    public List<Taxonomy> findByFamilyAndSubFamilyLike(String family, String subFamily) {
        return taxonomyRepository.findFirst1000ByFamilyAndSubFamilyLike(family, subFamily);
    }

    @Override
    public List<Taxonomy> findByFamilyAndGenusLike(String family, String genus) {
        return taxonomyRepository.findFirst1000ByFamilyAndGenusLike(family, genus);
    }

    @Override
    public List<Taxonomy> findByGenusAndSubGenusLike(String genus, String subGenus) {
        return taxonomyRepository.findFirst1000ByGenusAndSubGenusLike(genus, subGenus);
    }

    @Override
    public List<Taxonomy> findByGenusAndSpeciesLike(String family, String species) {
        return taxonomyRepository.findFirst1000ByGenusAndSpeciesLike(family, species);
    }

    @Override
    public List<Taxonomy> findBySpeciesAndSubSpeciesLike(String species, String subSpecies) {
        return taxonomyRepository.findFirst1000BySpeciesAndSubSpeciesLike(species, subSpecies);
    }

    @Override
    public Taxonomy findOne(Long organismId) {
        return taxonomyRepository.findByOrganismId(organismId);
    }


    @Override
    public List<Object> findAllTree(String kingdom,String phylumn,String tClass,String tOrder,String family,String genus) {
        //kingdom必传
        if (StringUtils.isNotBlank(kingdom)&&StringUtils.isNotBlank(phylumn)) {
            if (StringUtils.isNotBlank(tClass)){
                if (StringUtils.isNotBlank(tOrder)){
                    if (StringUtils.isNotBlank(family)){
                       return taxonomyRepository.getByFamilys(kingdom,phylumn,tClass,tOrder,family);
                    }
                    //根据界、门、纲、目获取科
                    return taxonomyRepository.getByTOrder(kingdom,phylumn,tClass,tOrder);
                }
                //根据界、门、纲获取目
                return taxonomyRepository.findByTClass(kingdom,phylumn,tClass);
            }
            //根据界、门获取纲
            return taxonomyRepository.findAllByPhylumn(kingdom,phylumn);
        }
        //根据界获取门
        return taxonomyRepository.findAllByKingdom(kingdom);
    }

    @Override
    public List<Object> findAllByKingdomAndKingdomLatin() {
        return taxonomyRepository.findAllByKingdomAndKingdomLatin();
    }


    @Override
    public List<Object> findAllByGenusTree(String name) {
        return taxonomyRepository.getAllByFamily(name);
    }

    @Override
    public List<BigInteger> findAllByOrganism(String kingdom, String phylumn, String tClass, String tOrder, String family, String genus) {
        if (StringUtils.isNotBlank(kingdom)&&StringUtils.isNotBlank(phylumn)){
                if (StringUtils.isNotBlank(tClass)){
                    if (StringUtils.isNotBlank(tOrder)){
                        if (StringUtils.isNotBlank(family)){
                            if (StringUtils.isNotBlank(genus)){
                                //获取界、门、纲、目、科、属下物种id
                                return taxonomyRepository.getByGenus(kingdom,phylumn,tClass,tOrder,family,genus);
                            }
                            //获取界、门、纲、目、科下物种id
                            return  taxonomyRepository.getByFamily(kingdom,phylumn,tClass,tOrder,family);
                        }
                        //获取界、门、纲、目下物种id
                       return taxonomyRepository.getByKingdomAndPhylumnAndTClassAndtOrder(kingdom,phylumn,tClass,tOrder);
                    }
                    //获取界、门、纲下物种id
                    return taxonomyRepository.getByKingdomAndPhylumnAndTClass(kingdom,phylumn,tClass);
                }
                //获取界、门下物种id
                return taxonomyRepository.getByKingdomAndPhylumn(kingdom,phylumn);
            }

        //获取界下物种id
        return taxonomyRepository.getByKingdom(kingdom);
    }

    @Override
    public List<BigInteger> getByKingdom(String kingdom) {
        return taxonomyRepository.getByKingdom(kingdom);
    }


}
